﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using System.Web;
namespace Microsoft.MCSUK.SPSiteDirectory2010
{
    public class ScanJobManager : LayoutsPageBase
    {
        protected System.Web.UI.WebControls.Literal ErrorMessageLiteral;
        protected Microsoft.SharePoint.WebControls.WebApplicationSelector WebAppSelector;
        protected Microsoft.SharePoint.WebControls.SchedulePicker JobSchedule;
        protected System.Web.UI.WebControls.CheckBox UseSiteOwnersGroup;
        protected System.Web.UI.WebControls.CheckBox UseManageWebPermission;
        protected System.Web.UI.WebControls.CheckBox UseSiteCollectionAdministrators;
        protected System.Web.UI.WebControls.Button OKButton;
        protected System.Web.UI.WebControls.Label WebAppStatus;
        protected System.Web.UI.WebControls.Button DisableButton;
        protected System.Web.UI.WebControls.TextBox ExclusionURLs;
        protected System.Web.UI.WebControls.CheckBox UseSiteLogo;
        protected System.Web.UI.WebControls.CheckBox IncludeSubSites;
        protected System.Web.UI.WebControls.CheckBox EnsureLogo;
        protected System.Web.UI.WebControls.TextBox DefaultLogoURL;
        protected void WebAppSelector_OnContextChange(object sender, EventArgs e)
        {
            GlobalMethods globalMethods = new GlobalMethods();
            try
            {
                if (WebAppSelector.CurrentItem != null)
                {
                    GlobalSolutionSettings globalSettings = globalMethods.GetOrCreateGlobalSolutionSettings(SPFarm.Local);
                    if (globalMethods.DoesSiteExist(globalSettings.SiteDirectoryURL))
                    {
                        SPFeature jobFeature = this.WebAppSelector.CurrentItem.Features[GlobalConstants.ScanJobFeatureID]; //get the job feature status on the web application and display text accordingly
                        if (jobFeature != null)
                        {
                            WebAppStatus.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_ScanJob_JobActivated").ToString();
                            ScanJobSettings scanJobSettings = globalMethods.GetOrCreateScanJobSettings(this.WebAppSelector.CurrentItem);
                            JobSchedule.Schedule = this.WebAppSelector.CurrentItem.JobDefinitions[scanJobSettings.JobDefinitionGUID].Schedule;
                            UseSiteOwnersGroup.Checked = scanJobSettings.UseSiteOwnersGroup;
                            UseManageWebPermission.Checked = scanJobSettings.UseManageWebPermission;
                            UseSiteCollectionAdministrators.Checked = scanJobSettings.UserSiteCollectionAdministrators;
                            ExclusionURLs.Text = scanJobSettings.ExclusionURLs;
                            UseSiteLogo.Checked = scanJobSettings.UseSiteLogo;
                            IncludeSubSites.Checked = scanJobSettings.IncludeSubSites;
                            EnsureLogo.Checked = scanJobSettings.EnsureSiteLogo;
                            DefaultLogoURL.Text = scanJobSettings.DefaultLogoURL;
                        }
                        else
                        {
                            WebAppStatus.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_ScanJob_JobNotActivated").ToString();
                        }
                    }
                    else //if the sitedirectory is not yet been configured. disable the ok button and throw an error message
                    {
                        OKButton.Enabled = false;
                        DisableButton.Enabled = false;
                        ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_Generic_SiteDirectoryNotConfigured").ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_GenericUIErrorText").ToString();
                globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
            }
        }

        protected void DisableButton_OnClick(object sender, EventArgs e)
        {
            GlobalMethods globalMethods = new GlobalMethods();
            try
            {
                ScanJobSettings scanJobSettings = globalMethods.GetOrCreateScanJobSettings(this.WebAppSelector.CurrentItem);
                if (scanJobSettings != null)
                {
                    if (scanJobSettings.IsScanJobConfigured)
                    {
                        globalMethods.DeactivateFeatureInWebApp(this.WebAppSelector.CurrentItem, GlobalConstants.ScanJobFeatureID);
                        scanJobSettings.Delete();
                    }
                }
                SPUtility.Redirect(GlobalConstants.CentralAdminRedirectURL, SPRedirectFlags.Trusted | SPRedirectFlags.DoNotEndResponse, this.Context);
            }
            catch (Exception ex)
            {
                ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_GenericUIErrorText").ToString(); 
                globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
            }
        }

        protected void OKButton_OnClick(object sender, EventArgs e)
        {
            GlobalMethods globalMethods = new GlobalMethods();
            try
            {
                if (Page.IsValid)
                {
                    if (WebAppSelector.CurrentItem != null)
                    {
                        Guid jobID = globalMethods.ReActivateFeatureInWebApp(this.WebAppSelector.CurrentItem, GlobalConstants.ScanJobFeatureID, GlobalConstants.ScanJobName, JobSchedule.Schedule);
                        ScanJobSettings scanJobSettings = globalMethods.GetOrCreateScanJobSettings(this.WebAppSelector.CurrentItem);
                        scanJobSettings.UseSiteOwnersGroup = UseSiteOwnersGroup.Checked;
                        scanJobSettings.UseManageWebPermission = UseManageWebPermission.Checked;
                        scanJobSettings.UserSiteCollectionAdministrators = UseSiteCollectionAdministrators.Checked;
                        scanJobSettings.IsScanJobConfigured = true;
                        scanJobSettings.JobDefinitionGUID = jobID;
                        scanJobSettings.ExclusionURLs = ExclusionURLs.Text;
                        scanJobSettings.UseSiteLogo = UseSiteLogo.Checked;
                        scanJobSettings.IncludeSubSites = IncludeSubSites.Checked;
                        scanJobSettings.EnsureSiteLogo = EnsureLogo.Checked;
                        scanJobSettings.DefaultLogoURL = DefaultLogoURL.Text;
                        scanJobSettings.Update();
                    }
                    SPUtility.Redirect(GlobalConstants.CentralAdminRedirectURL, SPRedirectFlags.Trusted | SPRedirectFlags.DoNotEndResponse, this.Context);
                }
                else
                {
                    ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "ADMIN_Generic_InvalidPageControls").ToString(); 
                }
            }
            catch (Exception ex)
            {
                ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_GenericUIErrorText").ToString();
                globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
            }
        }
    }
}
